package com.group12.liuxinpeng.duty.service.impl;

import com.group12.liuxinpeng.duty.entity.SearchDutyEntity;
import com.group12.liuxinpeng.duty.repository.DutyDao;
import com.group12.liuxinpeng.duty.service.DutyService;
import com.group12.liuxinpeng.mybatis.entity.PtDutyEntity;
import com.group12.liuxinpeng.mybatis.flexigrid.PadingRstType;
import com.group12.liuxinpeng.mybatis.flexigrid.PagingBean;
import com.group12.liuxinpeng.mybatis.ztree.ZtreeEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import com.group12.liuxinpeng.util.DataUtil;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@Service("DutyService")
public class DutyServiceImpl implements DutyService {
    @Resource
    private DutyDao dutyDao;

    @Override
    public PadingRstType<PtDutyEntity> queryDutyListPage(SearchDutyEntity search, PagingBean paging) {
        paging.deal(PtDutyEntity.class);
        PadingRstType<PtDutyEntity> ptDutyEntityPadingRstType = new PadingRstType<PtDutyEntity>();
        ptDutyEntityPadingRstType.setPage(paging.getPage());
        List<PtDutyEntity> list = dutyDao.queryDutyListPage(search,paging);
        ptDutyEntityPadingRstType.setRawRecords(list);
        Integer total = dutyDao.queryDutyTotal(search);
        ptDutyEntityPadingRstType.setTotal(total);
        ptDutyEntityPadingRstType.putItems();
        return ptDutyEntityPadingRstType;
    }


    @Resource
    private MessageSource messageSource;
    @Override
    public List<ZtreeEntity> getMenuTree() {
        List<ZtreeEntity> list = dutyDao.queryMenuTree();
        ZtreeEntity ZtreeEntity = new ZtreeEntity();
        ZtreeEntity.setId("0");
        Locale locale = LocaleContextHolder.getLocale();
        String unboundName = messageSource.getMessage("common.unbound.name",null,locale);
        ZtreeEntity.setName(unboundName);
        list.add(ZtreeEntity);
        Integer index = 0;
        for(ZtreeEntity item:list){
            if(index < 10){
                item.setOpen(true);
            }else{
                item.setOpen(false);
            }
            index++;
        }

        return list;
    }
    public String getRoleIdByName(String roleName){
        return dutyDao.getRoleIdByName(roleName);
    }
    public String getOrganIdByName(String organName){
        return dutyDao.getRoleIdByName(organName);
    }

    @Override
    public void dutyAdd(PtDutyEntity ptDutyEntity) {
        ptDutyEntity.setDutyId(DataUtil.getUUIDShort());
        ptDutyEntity.setOrginUuid(dutyDao.getOrganIdByName(ptDutyEntity.getOrganName()));
        ptDutyEntity.setRoleUuid(dutyDao.getRoleIdByName(ptDutyEntity.getRoleName()));
        dutyDao.dutyAdd(ptDutyEntity);
    }

    public void dutyDelete(String dutyId){
        dutyDao.dutyDelete(dutyId);
    }
    public void dutyUpdate(PtDutyEntity ptDutyEntity){
        ptDutyEntity.setOrginUuid(dutyDao.getOrganIdByName(ptDutyEntity.getOrganName()));
        ptDutyEntity.setRoleUuid(dutyDao.getRoleIdByName(ptDutyEntity.getRoleName()));
        dutyDao.dutyUpdate(ptDutyEntity);
    }
}
